const { regeneratorRuntime, dayjs } = global


Page(global.connect(
  state => ({
    ...state.globalReducer,
    ...state.activityReducer
  }),
  dispatch => ({
    async makeOrder(data) {
      return await dispatch(global.activityReducer.makeOrder(data))
    },
    async prepareEffectImage(data) {
      return await dispatch(global.globalReducer.prepareEffectImage(data))
    },
  })
)({
  onLoad() {
    console.log(this.route)
  },
  handleLicon1Tap() {
    wx.navigateBack()
  },
  handleImageView(e) {
    const { image_url, image_name } = e.currentTarget.dataset.detail
    const urls = [{
      name: image_name,
      url: image_url,
    }]
    this.prepareEffectImage(urls)
    global.handleRoute({ route: 'effect' })
  },
  handleNextStep(e) {
    console.log('handleNextStep', e)
    const { order_no, up_time, down_time, uiActivityUpTime, uiActivityDownTime } = this.data.orderDetail.data
    // 订单编号
    const formData = {
      order_no
    }
    // 上画日期
    if (Number(up_time)) {
      formData.up_time = dayjs(uiActivityUpTime).unix()
    }
    // 是否下画 下画日期
    if (Number(down_time)) {
      formData.is_down = true
      formData.down_time = dayjs(uiActivityDownTime).unix()
    } else {
      formData.is_down = false
    }
    // 广告位json
    const json = []
    this.data.storePosition.data.filter(i => i.selected).map(i => {
      const obj = {}
      obj.id = i.id
      obj.ads_id = i.ads_id
      obj.ads_type = i.ads_type
      if (i.ads_type == 1) {
        obj.num = i.tpl.num
        const detail = i.tpl.detail.map(j => {
          return {
            id: j.id,
            ads_detail_id: j.ads_detail_id,
            remark: j.remark,
            materiel_id: j.materiel_id,
            activity_image_id: j.image_id,
          }
        })
        obj.ads_detail = detail
      }
      if (i.ads_type == 2) {
        obj.num = i.num
        // delete obj.ads_id
        obj.ads_detail = [{
          id: i.id,
          ads_name: i.ads_name,
          remark: i.remark,
          ads_type: i.ads_type,
          width: i.width,
          height: i.height,
          materiel_id: i.materiel_id,
          activity_image_id: i.image_id,
          is_upper_air: i.is_upper_air,
        }]
      }
      json.push(obj)
      return obj
    })
    formData.json = JSON.stringify(json)

    this.makeOrder(formData).then(res => {
      console.log(res)
      if (res.actionStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: '下单成功'
        })
        global.handleRoute({ route: 'index' })
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '下单失败'
        })
      }
    }).catch(err => {
      console.error(err)
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || '下单失败'
      })
    })
    // wx.navigateBack({
    //   complete() {
    //     wx.navigateBack({
    //       complete() {
    //         wx.navigateBack({
    //           complete() {
    //             // wx.navigateBack({
    //               // complete() {
    //                 global.handleRoute({ route: 'order-detail' })
    //               // }
    //             // })
    //           }
    //         })
    //       }
    //     })
    //     console.log('reLaunch complete')
    //     global.handleRoute({ route: 'order-detail' })
    //   }
    // })
  }
}))